/**
 * 
 */
package net.oschina.j2cache.hibernate3;

import static org.junit.Assert.assertTrue;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Criterion;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



/**
 * @author liao
 *
 */

public class J2CacheProviderTester1 {
	
	
	private static final Logger LOG = LoggerFactory.getLogger(J2CacheProviderTester1.class);
    
    static SessionFactory sessionFactory;

	private Session session;
    
    static {
        Configuration configuration = new Configuration();
        sessionFactory = configuration.configure().buildSessionFactory();
    }
    
    @Before
    public void testInsert() {
        Session session = getSession();
        session.beginTransaction();
        try {
            User user = new User();
            user.setId(1);
            user.setName("tom");
            session.save(user);
            
            user = new User();
            user.setId(2);
            user.setName("john");
            session.save(user);
            user = (User) session.get(User.class, 1);
            LOG.info("缓存检索，user={}",user);
            user = (User) session.get(User.class, 2);
            LOG.info("缓存检索，user={}",user);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        } finally {
//            session.close();
        }
    }
    
    @After
    public void testDelete() {
        Session session = getSession();
        session.beginTransaction();
        try {
            User user = (User) session.get(User.class, 1);
            assertTrue(user != null);
            session.delete(user);
            
            user = (User) session.get(User.class, 2);
            assertTrue(user != null);
            session.delete(user);
            
            session.createQuery("delete from User").executeUpdate();
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        } finally {
            session.close();
        }
    }
    
    @Test
    public void testQuery() {
        Session session = getSession();
        try {
        	User user = (User) session.get(User.class, 1);
        	assertTrue(user.getId() == 1);
        	user = (User) session.get(User.class, 1);
        	LOG.info("从缓存检索，user ={}",user);
        	assertTrue(user.getName().equals("tom"));
        	user = (User) session.get(User.class, 1);
        	LOG.info("zai从缓存检索，user ={}",user);
			
		} finally {
			// TODO: handle finally clause
//			session.close();
		}
    }
    @Test
    public void tetfind() {
    	Session session = getSession();
        try {
			// 查询list
			List<User> articleList = this.find();
			LOG.info("第一次查询,保存查询结果到缓存:" + articleList);
			articleList = this.find();
			LOG.info("第二次查询，从缓存中获取:" + articleList);
			session.beginTransaction();
			// 修改数据后
			User saveartice = articleList.get(0);
//			saveartice.setId(articleList.get(0).getId());
			saveartice.setName("测试缓存1");
			session.saveOrUpdate(saveartice);
			// 缓存数据会删除
			session.getTransaction().commit();
			articleList = this.find();
			LOG.info("再次查询,缓存查询结果:" + articleList);// 会重新缓存结果
			session.beginTransaction();
			// 测试新增数据
			User newartice = new User();
			newartice.setName("测试缓存2");
			session.save(newartice);
			// 缓存数据会删除5
			session.getTransaction().commit();
			articleList = this.find();
			LOG.info("再次查询,缓存查询结果:" + articleList);// 会重新缓存结果
			assert articleList.size() == 3;
			articleList = this.find();
			LOG.info("第N次查询，从缓存中获取:" + articleList);
		} finally {
			// TODO: handle finally clause
//			session.close();
		}

    }

	public Session getSession() {
		if (session == null) {
			session = sessionFactory.openSession();
		}
		return session;
	}
    
    public List<User> find(Criterion... criterions) {
        /*Criteria criteria = getSession().createCriteria(User.class);
        for (Criterion c : criterions) {
            criteria.add(c);
        }
        criteria.setCacheable(true);
        return criteria.list();*/
        
        return getSession().createQuery("from User").setCacheable(true).list();
    }
    
}
